package com.example.spikeRuota;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

public class WheelQuarter extends View{

	
	int dimension;

	Paint paintBack;
	Paint paintText;
	Path path;
	Path arc;
	int light;
	int dark;
	int left, top, bottom, right, cx, cy;
	int angle, sweep, distance;
	String text;
	int[] colors;
	float[] pos; 
	
	
	public WheelQuarter(Context c, AttributeSet attrs){
		super(c, attrs);
		//get attributes
		TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.WheelQuarter);
		dimension = a.getInt(R.styleable.WheelQuarter_dimension, 100);
		dark = a.getColor(R.styleable.WheelQuarter_dark, 0xff0000);
		light = a.getColor(R.styleable.WheelQuarter_light, 0x0000ff);
		//non serve salvare l'orientamento, la getOrientation imposta gli altri campi in modo conforme
		getOrientation(a.getString(R.styleable.WheelQuarter_orientation));
		text = a.getString(R.styleable.WheelQuarter_text);
		
		
		//gradient parameters
		colors = new int[] { 
	            light, 
	            light,
	            dark
	        };
		pos = new float[] {0,0.7f,1};
		
		paintBack = new Paint();
		paintBack.setAntiAlias(true);
		
		paintText = new Paint();
		paintText.setAntiAlias(true);
		paintText.setColor(getResources().getColor(R.color.ruotaTesto));
		paintText.setTextSize(20);
		
		path = new Path();
		arc = new Path();
		
		this.setClickable(true);
		
	}


	@Override
	protected void onDraw(Canvas c){
		arc.addArc(new RectF(left, top, right, bottom), angle, sweep);
		
		path.addPath(arc);
		path.lineTo(cx, cy);
		path.close();
		
		RadialGradient rg = new RadialGradient(cx, cy, dimension, colors, pos, Shader.TileMode.REPEAT);
		paintBack.setShader(rg);
		c.drawPath(path, paintBack);
		
		c.drawTextOnPath(text, arc, 20, distance, paintText);	
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(dimension, dimension);
		setMeasuredDimension(dimension, dimension);
	}

	private void getOrientation(String string) {
		int defSweep = 90;
		
		if (string.equals("nw")){
			angle = 180;
			sweep = defSweep;
			distance = 20;
			left=0; top=0; right=dimension*2; bottom=dimension*2; 
			cx=dimension; cy=dimension;
		}
		else if (string.equals("ne")){
			angle = 270;
			sweep = defSweep;
			distance = 20;
			left=-dimension; top=0; right=dimension; bottom=dimension*2;
			cx=0; cy=dimension;
		}
		else if (string.equals("se")){
			angle = 90;
			sweep = -defSweep;
			distance = -10;
			left=-dimension; top=-dimension; right=dimension; bottom=dimension;
			cx=0; cy=0;
		}
		else if (string.equals("sw")){
			angle = 180;
			sweep = -defSweep;
			distance = -10;
			left=0; top=-dimension; right=dimension*2; bottom=dimension;
			cx=dimension; cy=0;
		}
		else{
			angle = 0;
			sweep = defSweep;
			distance = 0;
			top = left = right = bottom = cx = cy = 0;
		}
	}

}
